package com.ckzp.weixin;

import com.alibaba.fastjson.JSON;
import com.ckzp.core.ConfigUtil;
import com.jfinal.kit.HttpKit;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 51594 on 2022/8/15.
 */
public class WxTool {

    /**
     * media地址
     */
    public static String UP_URL = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=[ACCESS_TOKEN]&media_id=";


    /**
     * @return
     */
    public static String getAppID() {
        return ConfigUtil.getProperty("WX_APPID");
    }

    /**
     * @return
     */
    public static String getAppID2() {
        return ConfigUtil.getProperty("WX_APPID2");
    }


    /**
     * @return
     */
    public static String getAppSecret() {
        return ConfigUtil.getProperty("WX_APPSECRET");
    }


    /**
     * @return
     */
    public static String getAppSecret2() {
        return ConfigUtil.getProperty("WX_APPSECRET2");
    }

    /**
     * ]
     *
     * @param return_url
     * @return
     */
    public static String EncodeUrl(String return_url) {
        try {
            return java.net.URLEncoder.encode(return_url, "gb2312");
        } catch (Exception e) {
            return return_url;
        }
    }


    /**
     * 报名跳验证地址
     *
     * @param return_url
     * @return
     */
    public static String getUserAuthUrl(String return_url) {
        return_url = WxTool.EncodeUrl(return_url);
        String appid = getAppID();
        String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid
                + "&redirect_uri=" + return_url + "&response_type=code&scope=snsapi_base&state=9981#wechat_redirect";
        return url;
    }

    /**
     * 报名跳验证地址
     *
     * @param return_url
     * @return
     */
    public static String getUserAuthUrl2(String return_url) {
        return_url = WxTool.EncodeUrl(return_url);
        String appid = getAppID2();
        String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid
                + "&redirect_uri=" + return_url + "&response_type=code&scope=snsapi_base&state=9981#wechat_redirect";
        return url;
    }


    /**
     * 判断是否微信环境
     *
     * @param request
     * @return
     */
    public static boolean isWx(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        if (userAgent == null) userAgent = "";
        userAgent = userAgent.toLowerCase();
        if (userAgent.indexOf("micromessenger") > -1) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 取得用户的头像尼称等信息
     *
     * @param openid
     * @return
     */
    public static Map getUserInfo(String openid) {
        String ACCESS_TOKEN = AccessTokenTool.getAccessToken();
        String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + ACCESS_TOKEN + "&openid=" + openid + "&lang=zh_CN";
        String htmls = HttpKit.get(url).toString();
        return JSON.parseObject(htmls, Map.class);
    }


    /**
     * 发送业务提醒模板消息
     *
     * @param openid
     * @return
     */
    public static Map sendWeixinMessage(String template_id, String openid, Map datas) {
        return sendWeixinMessage(template_id, openid, datas, "");
    }

    /**
     * 发送业务提醒模板消息
     *
     * @param openid
     * @return
     */
    public static Map sendWeixinMessage(String template_id, String openid, Map datas, String urls) {
        try {
            String ACCESS_TOKEN = AccessTokenTool.getAccessToken();
            String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + ACCESS_TOKEN;
            Map parmas = new HashMap();
            parmas.put("touser", openid);
            parmas.put("template_id", template_id);
            parmas.put("url", urls);
            parmas.put("data", datas);
            String htmls = HttpKit.post(url, JSON.toJSONString(parmas));
            return JSON.parseObject(htmls, Map.class);
        } catch (Exception e) {
            Map ms = new HashMap();
            ms.put("errcode", 9);
            ms.put("errmsg", e.getMessage());
            return ms;
        }
    }

    /**
     * 发送业务提醒模板消息
     *
     * @param openid
     * @return
     */
    public static Map sendWeixinMessage2(String template_id, String openid, Map datas, String urls) {
        try {
            String ACCESS_TOKEN = AccessTokenTool.getAccessToken();
            String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + ACCESS_TOKEN;
            Map parmas = new HashMap();
            parmas.put("touser", openid);
            parmas.put("template_id", template_id);
            parmas.put("url", urls);
            parmas.put("data", datas);
            String htmls = HttpKit.post(url, JSON.toJSONString(parmas));
            return JSON.parseObject(htmls, Map.class);
        } catch (Exception e) {
            Map ms = new HashMap();
            ms.put("errcode", 9);
            ms.put("errmsg", e.getMessage());
            return ms;
        }
    }

    /**
     * 更新微信备注名
     *
     * @param openid
     * @param mark_name
     * @return
     */
    public static Map updateUserRemark(String openid, String mark_name) {
        String ACCESS_TOKEN = AccessTokenTool.getAccessToken();
        String url = "https://api.weixin.qq.com/cgi-bin/user/info/updateremark?access_token=" + ACCESS_TOKEN;
        Map parmas = new HashMap();
        parmas.put("openid", openid);
        parmas.put("remark", mark_name);
        String htmls = HttpKit.post(url, JSON.toJSONString(parmas));
        return JSON.parseObject(htmls, Map.class);
    }

    public static Map getDataMap(String values, String colors) {
        Map<String, String> m = new HashMap<String, String>();
        m.put("value", values);
        m.put("color", colors);
        return m;
    }

    public static String getUpUrl() {
        String ACCESS_TOKEN = AccessTokenTool.getAccessToken();
        String urls = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=" + ACCESS_TOKEN + "&media_id=";
        return urls;
    }


    public static String getUpUrl2() {
        String ACCESS_TOKEN = AccessTokenTool.getAccessToken2();
        String urls = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=" + ACCESS_TOKEN + "&media_id=";
        return urls;
    }


}
